{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "ecad719c",
   "metadata": {},
   "source": [
    "# Using Weights & Biases with Tune\n",
    "\n",
    "<a id=\"try-anyscale-quickstart-tune-wandb\" href=\"https://console.anyscale.com/register/ha?render_flow=ray&utm_source=ray_docs&utm_medium=docs&utm_campaign=tune-wandb\">\n",
    "    <img src=\"../../_static/img/run-on-anyscale.svg\" alt=\"try-anyscale-quickstart\">\n",
    "</a>\n",
    "<br></br>\n",
    "\n",
    "(tune-wandb-ref)=\n",
    "\n",
    "[Weights & Biases](https://www.wandb.ai/) (Wandb) is a tool for experiment\n",
    "tracking, model optimizaton, and dataset versioning. It is very popular\n",
    "in the machine learning and data science community for its superb visualization\n",
    "tools.\n",
    "\n",
    "```{image} /images/wandb_logo_full.png\n",
    ":align: center\n",
    ":alt: Weights & Biases\n",
    ":height: 80px\n",
    ":target: https://www.wandb.ai/\n",
    "```\n",
    "\n",
    "Ray Tune currently offers two lightweight integrations for Weights & Biases.\n",
    "One is the {ref}`WandbLoggerCallback <air-wandb-logger>`, which automatically logs\n",
    "metrics reported to Tune to the Wandb API.\n",
    "\n",
    "The other one is the {ref}`setup_wandb() <air-wandb-setup>` function, which can be\n",
    "used with the function API. It automatically\n",
    "initializes the Wandb API with Tune's training information. You can just use the\n",
    "Wandb API like you would normally do, e.g. using `wandb.log()` to log your training\n",
    "process.\n",
    "\n",
    "```{contents}\n",
    ":backlinks: none\n",
    ":local: true\n",
    "```\n",
    "\n",
    "## Running A Weights & Biases Example\n",
    "\n",
    "In the following example we're going to use both of the above methods, namely the `WandbLoggerCallback` and\n",
    "the `setup_wandb` function to log metrics.\n",
    "\n",
    "As the very first step, make sure you're logged in into wandb on all machines you're running your training on:\n",
    "\n",
    "    wandb login\n",
    "\n",
    "We can then start with a few crucial imports:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "100bcf8a",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "import ray\n",
    "from ray import tune\n",
    "from ray.air.integrations.wandb import WandbLoggerCallback, setup_wandb\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "9346c0f6",
   "metadata": {},
   "source": [
    "Next, let's define an easy `train_function` function (a Tune `Trainable`) that reports a random loss to Tune.\n",
    "The objective function itself is not important for this example, since we want to focus on the Weights & Biases\n",
    "integration primarily."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "e8b4fc4d",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "def train_function(config):\n",
    "    for i in range(30):\n",
    "        loss = config[\"mean\"] + config[\"sd\"] * np.random.randn()\n",
    "        tune.report({\"loss\": loss})\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "831eed42",
   "metadata": {},
   "source": [
    "You can define a\n",
    "simple grid-search Tune run using the `WandbLoggerCallback` as follows:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "52988599",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "def tune_with_callback():\n",
    "    \"\"\"Example for using a WandbLoggerCallback with the function API\"\"\"\n",
    "    tuner = tune.Tuner(\n",
    "        train_function,\n",
    "        tune_config=tune.TuneConfig(\n",
    "            metric=\"loss\",\n",
    "            mode=\"min\",\n",
    "        ),\n",
    "        run_config=tune.RunConfig(\n",
    "            callbacks=[WandbLoggerCallback(project=\"Wandb_example\")]\n",
    "        ),\n",
    "        param_space={\n",
    "            \"mean\": tune.grid_search([1, 2, 3, 4, 5]),\n",
    "            \"sd\": tune.uniform(0.2, 0.8),\n",
    "        },\n",
    "    )\n",
    "    tuner.fit()\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "e24c05fa",
   "metadata": {},
   "source": [
    "To use the `setup_wandb` utility, you simply call this function in your objective.\n",
    "Note that we also use `wandb.log(...)` to log the `loss` to Weights & Biases as a dictionary.\n",
    "Otherwise, this version of our objective is identical to its original."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "5e30d5e7",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "def train_function_wandb(config):\n",
    "    wandb = setup_wandb(config, project=\"Wandb_example\")\n",
    "\n",
    "    for i in range(30):\n",
    "        loss = config[\"mean\"] + config[\"sd\"] * np.random.randn()\n",
    "        tune.report({\"loss\": loss})\n",
    "        wandb.log(dict(loss=loss))\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "04040bcb",
   "metadata": {},
   "source": [
    "With the `train_function_wandb` defined, your Tune experiment will set up `wandb` in each trial once it starts!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d4fbd368",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "def tune_with_setup():\n",
    "    \"\"\"Example for using the setup_wandb utility with the function API\"\"\"\n",
    "    tuner = tune.Tuner(\n",
    "        train_function_wandb,\n",
    "        tune_config=tune.TuneConfig(\n",
    "            metric=\"loss\",\n",
    "            mode=\"min\",\n",
    "        ),\n",
    "        param_space={\n",
    "            \"mean\": tune.grid_search([1, 2, 3, 4, 5]),\n",
    "            \"sd\": tune.uniform(0.2, 0.8),\n",
    "        },\n",
    "    )\n",
    "    tuner.fit()\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "f9521481",
   "metadata": {},
   "source": [
    "Finally, you can also define a class-based Tune `Trainable` by using the `setup_wandb` in the `setup()` method and storing the run object as an attribute. Please note that with the class trainable, you have to pass the trial id, name, and group separately:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "d27a7a35",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "class WandbTrainable(tune.Trainable):\n",
    "    def setup(self, config):\n",
    "        self.wandb = setup_wandb(\n",
    "            config,\n",
    "            trial_id=self.trial_id,\n",
    "            trial_name=self.trial_name,\n",
    "            group=\"Example\",\n",
    "            project=\"Wandb_example\",\n",
    "        )\n",
    "\n",
    "    def step(self):\n",
    "        for i in range(30):\n",
    "            loss = self.config[\"mean\"] + self.config[\"sd\"] * np.random.randn()\n",
    "            self.wandb.log({\"loss\": loss})\n",
    "        return {\"loss\": loss, \"done\": True}\n",
    "\n",
    "    def save_checkpoint(self, checkpoint_dir: str):\n",
    "        pass\n",
    "\n",
    "    def load_checkpoint(self, checkpoint_dir: str):\n",
    "        pass\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "fa189bb2",
   "metadata": {},
   "source": [
    "Running Tune with this `WandbTrainable` works exactly the same as with the function API.\n",
    "The below `tune_trainable` function differs from `tune_decorated` above only in the first argument we pass to\n",
    "`Tuner()`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "6e546cc2",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "def tune_trainable():\n",
    "    \"\"\"Example for using a WandTrainableMixin with the class API\"\"\"\n",
    "    tuner = tune.Tuner(\n",
    "        WandbTrainable,\n",
    "        tune_config=tune.TuneConfig(\n",
    "            metric=\"loss\",\n",
    "            mode=\"min\",\n",
    "        ),\n",
    "        param_space={\n",
    "            \"mean\": tune.grid_search([1, 2, 3, 4, 5]),\n",
    "            \"sd\": tune.uniform(0.2, 0.8),\n",
    "        },\n",
    "    )\n",
    "    results = tuner.fit()\n",
    "\n",
    "    return results.get_best_result().config\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "0b736172",
   "metadata": {},
   "source": [
    "Since you may not have an API key for Wandb, we can _mock_ the Wandb logger and test all three of our training\n",
    "functions as follows.\n",
    "If you are logged in into wandb, you can set `mock_api = False` to actually upload your results to Weights & Biases."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "e0e7f481",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2022-11-02 16:02:45,355\tINFO worker.py:1534 -- Started a local Ray instance. View the dashboard at \u001b[1m\u001b[32mhttp://127.0.0.1:8266 \u001b[39m\u001b[22m\n",
      "2022-11-02 16:02:46,513\tINFO wandb.py:282 -- Already logged into W&B.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div class=\"tuneStatus\">\n",
       "  <div style=\"display: flex;flex-direction: row\">\n",
       "    <div style=\"display: flex;flex-direction: column;\">\n",
       "      <h3>Tune Status</h3>\n",
       "      <table>\n",
       "<tbody>\n",
       "<tr><td>Current time:</td><td>2022-11-02 16:03:13</td></tr>\n",
       "<tr><td>Running for: </td><td>00:00:27.28        </td></tr>\n",
       "<tr><td>Memory:      </td><td>10.8/16.0 GiB      </td></tr>\n",
       "</tbody>\n",
       "</table>\n",
       "    </div>\n",
       "    <div class=\"vDivider\"></div>\n",
       "    <div class=\"systemInfo\">\n",
       "      <h3>System Info</h3>\n",
       "      Using FIFO scheduling algorithm.<br>Resources requested: 0/16 CPUs, 0/0 GPUs, 0.0/3.44 GiB heap, 0.0/1.72 GiB objects\n",
       "    </div>\n",
       "    \n",
       "  </div>\n",
       "  <div class=\"hDivider\"></div>\n",
       "  <div class=\"trialStatus\">\n",
       "    <h3>Trial Status</h3>\n",
       "    <table>\n",
       "<thead>\n",
       "<tr><th>Trial name                </th><th>status    </th><th>loc            </th><th style=\"text-align: right;\">  mean</th><th style=\"text-align: right;\">      sd</th><th style=\"text-align: right;\">  iter</th><th style=\"text-align: right;\">  total time (s)</th><th style=\"text-align: right;\">    loss</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "<tr><td>train_function_7676d_00000</td><td>TERMINATED</td><td>127.0.0.1:14578</td><td style=\"text-align: right;\">     1</td><td style=\"text-align: right;\">0.411212</td><td style=\"text-align: right;\">    30</td><td style=\"text-align: right;\">        0.236137</td><td style=\"text-align: right;\">0.828527</td></tr>\n",
       "<tr><td>train_function_7676d_00001</td><td>TERMINATED</td><td>127.0.0.1:14591</td><td style=\"text-align: right;\">     2</td><td style=\"text-align: right;\">0.756339</td><td style=\"text-align: right;\">    30</td><td style=\"text-align: right;\">        5.57185 </td><td style=\"text-align: right;\">3.13156 </td></tr>\n",
       "<tr><td>train_function_7676d_00002</td><td>TERMINATED</td><td>127.0.0.1:14593</td><td style=\"text-align: right;\">     3</td><td style=\"text-align: right;\">0.436643</td><td style=\"text-align: right;\">    30</td><td style=\"text-align: right;\">        5.50237 </td><td style=\"text-align: right;\">3.26679 </td></tr>\n",
       "<tr><td>train_function_7676d_00003</td><td>TERMINATED</td><td>127.0.0.1:14595</td><td style=\"text-align: right;\">     4</td><td style=\"text-align: right;\">0.295929</td><td style=\"text-align: right;\">    30</td><td style=\"text-align: right;\">        5.60986 </td><td style=\"text-align: right;\">3.70388 </td></tr>\n",
       "<tr><td>train_function_7676d_00004</td><td>TERMINATED</td><td>127.0.0.1:14596</td><td style=\"text-align: right;\">     5</td><td style=\"text-align: right;\">0.335292</td><td style=\"text-align: right;\">    30</td><td style=\"text-align: right;\">        5.61385 </td><td style=\"text-align: right;\">4.74294 </td></tr>\n",
       "</tbody>\n",
       "</table>\n",
       "  </div>\n",
       "</div>\n",
       "<style>\n",
       ".tuneStatus {\n",
       "  color: var(--jp-ui-font-color1);\n",
       "}\n",
       ".tuneStatus .systemInfo {\n",
       "  display: flex;\n",
       "  flex-direction: column;\n",
       "}\n",
       ".tuneStatus td {\n",
       "  white-space: nowrap;\n",
       "}\n",
       ".tuneStatus .trialStatus {\n",
       "  display: flex;\n",
       "  flex-direction: column;\n",
       "}\n",
       ".tuneStatus h3 {\n",
       "  font-weight: bold;\n",
       "}\n",
       ".tuneStatus .hDivider {\n",
       "  border-bottom-width: var(--jp-border-width);\n",
       "  border-bottom-color: var(--jp-border-color0);\n",
       "  border-bottom-style: solid;\n",
       "}\n",
       ".tuneStatus .vDivider {\n",
       "  border-left-width: var(--jp-border-width);\n",
       "  border-left-color: var(--jp-border-color0);\n",
       "  border-left-style: solid;\n",
       "  margin: 0.5em 1em 0.5em 1em;\n",
       "}\n",
       "</style>\n"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div class=\"trialProgress\">\n",
       "  <h3>Trial Progress</h3>\n",
       "  <table>\n",
       "<thead>\n",
       "<tr><th>Trial name                </th><th>date               </th><th>done  </th><th>episodes_total  </th><th>experiment_id                   </th><th>experiment_tag    </th><th>hostname            </th><th style=\"text-align: right;\">  iterations_since_restore</th><th style=\"text-align: right;\">    loss</th><th>node_ip  </th><th style=\"text-align: right;\">  pid</th><th style=\"text-align: right;\">  time_since_restore</th><th style=\"text-align: right;\">  time_this_iter_s</th><th style=\"text-align: right;\">  time_total_s</th><th style=\"text-align: right;\">  timestamp</th><th style=\"text-align: right;\">  timesteps_since_restore</th><th>timesteps_total  </th><th style=\"text-align: right;\">  training_iteration</th><th>trial_id   </th><th style=\"text-align: right;\">  warmup_time</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "<tr><td>train_function_7676d_00000</td><td>2022-11-02_16-02-53</td><td>True  </td><td>                </td><td>a9f242fa70184d9dadd8952b16fb0ecc</td><td>0_mean=1,sd=0.4112</td><td>Kais-MBP.local.meter</td><td style=\"text-align: right;\">                        30</td><td style=\"text-align: right;\">0.828527</td><td>127.0.0.1</td><td style=\"text-align: right;\">14578</td><td style=\"text-align: right;\">            0.236137</td><td style=\"text-align: right;\">        0.00381589</td><td style=\"text-align: right;\">      0.236137</td><td style=\"text-align: right;\"> 1667430173</td><td style=\"text-align: right;\">                        0</td><td>                 </td><td style=\"text-align: right;\">                  30</td><td>7676d_00000</td><td style=\"text-align: right;\">   0.00366998</td></tr>\n",
       "<tr><td>train_function_7676d_00001</td><td>2022-11-02_16-03-03</td><td>True  </td><td>                </td><td>f57118365bcb4c229fe41c5911f05ad6</td><td>1_mean=2,sd=0.7563</td><td>Kais-MBP.local.meter</td><td style=\"text-align: right;\">                        30</td><td style=\"text-align: right;\">3.13156 </td><td>127.0.0.1</td><td style=\"text-align: right;\">14591</td><td style=\"text-align: right;\">            5.57185 </td><td style=\"text-align: right;\">        0.00627518</td><td style=\"text-align: right;\">      5.57185 </td><td style=\"text-align: right;\"> 1667430183</td><td style=\"text-align: right;\">                        0</td><td>                 </td><td style=\"text-align: right;\">                  30</td><td>7676d_00001</td><td style=\"text-align: right;\">   0.0027349 </td></tr>\n",
       "<tr><td>train_function_7676d_00002</td><td>2022-11-02_16-03-03</td><td>True  </td><td>                </td><td>394021d4515d4616bae7126668f73b2b</td><td>2_mean=3,sd=0.4366</td><td>Kais-MBP.local.meter</td><td style=\"text-align: right;\">                        30</td><td style=\"text-align: right;\">3.26679 </td><td>127.0.0.1</td><td style=\"text-align: right;\">14593</td><td style=\"text-align: right;\">            5.50237 </td><td style=\"text-align: right;\">        0.00494576</td><td style=\"text-align: right;\">      5.50237 </td><td style=\"text-align: right;\"> 1667430183</td><td style=\"text-align: right;\">                        0</td><td>                 </td><td style=\"text-align: right;\">                  30</td><td>7676d_00002</td><td style=\"text-align: right;\">   0.00286222</td></tr>\n",
       "<tr><td>train_function_7676d_00003</td><td>2022-11-02_16-03-03</td><td>True  </td><td>                </td><td>a575e79c9d95485fa37deaa86267aea4</td><td>3_mean=4,sd=0.2959</td><td>Kais-MBP.local.meter</td><td style=\"text-align: right;\">                        30</td><td style=\"text-align: right;\">3.70388 </td><td>127.0.0.1</td><td style=\"text-align: right;\">14595</td><td style=\"text-align: right;\">            5.60986 </td><td style=\"text-align: right;\">        0.00689816</td><td style=\"text-align: right;\">      5.60986 </td><td style=\"text-align: right;\"> 1667430183</td><td style=\"text-align: right;\">                        0</td><td>                 </td><td style=\"text-align: right;\">                  30</td><td>7676d_00003</td><td style=\"text-align: right;\">   0.00299597</td></tr>\n",
       "<tr><td>train_function_7676d_00004</td><td>2022-11-02_16-03-03</td><td>True  </td><td>                </td><td>91ce57dcdbb54536b1874666b711350d</td><td>4_mean=5,sd=0.3353</td><td>Kais-MBP.local.meter</td><td style=\"text-align: right;\">                        30</td><td style=\"text-align: right;\">4.74294 </td><td>127.0.0.1</td><td style=\"text-align: right;\">14596</td><td style=\"text-align: right;\">            5.61385 </td><td style=\"text-align: right;\">        0.00672579</td><td style=\"text-align: right;\">      5.61385 </td><td style=\"text-align: right;\"> 1667430183</td><td style=\"text-align: right;\">                        0</td><td>                 </td><td style=\"text-align: right;\">                  30</td><td>7676d_00004</td><td style=\"text-align: right;\">   0.00323987</td></tr>\n",
       "</tbody>\n",
       "</table>\n",
       "</div>\n",
       "<style>\n",
       ".trialProgress {\n",
       "  display: flex;\n",
       "  flex-direction: column;\n",
       "  color: var(--jp-ui-font-color1);\n",
       "}\n",
       ".trialProgress h3 {\n",
       "  font-weight: bold;\n",
       "}\n",
       ".trialProgress td {\n",
       "  white-space: nowrap;\n",
       "}\n",
       "</style>\n"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2022-11-02 16:03:13,913\tINFO tune.py:788 -- Total run time: 28.53 seconds (27.28 seconds for the tuning loop).\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div class=\"tuneStatus\">\n",
       "  <div style=\"display: flex;flex-direction: row\">\n",
       "    <div style=\"display: flex;flex-direction: column;\">\n",
       "      <h3>Tune Status</h3>\n",
       "      <table>\n",
       "<tbody>\n",
       "<tr><td>Current time:</td><td>2022-11-02 16:03:22</td></tr>\n",
       "<tr><td>Running for: </td><td>00:00:08.49        </td></tr>\n",
       "<tr><td>Memory:      </td><td>9.9/16.0 GiB       </td></tr>\n",
       "</tbody>\n",
       "</table>\n",
       "    </div>\n",
       "    <div class=\"vDivider\"></div>\n",
       "    <div class=\"systemInfo\">\n",
       "      <h3>System Info</h3>\n",
       "      Using FIFO scheduling algorithm.<br>Resources requested: 0/16 CPUs, 0/0 GPUs, 0.0/3.44 GiB heap, 0.0/1.72 GiB objects\n",
       "    </div>\n",
       "    \n",
       "  </div>\n",
       "  <div class=\"hDivider\"></div>\n",
       "  <div class=\"trialStatus\">\n",
       "    <h3>Trial Status</h3>\n",
       "    <table>\n",
       "<thead>\n",
       "<tr><th>Trial name                      </th><th>status    </th><th>loc            </th><th style=\"text-align: right;\">  mean</th><th style=\"text-align: right;\">      sd</th><th style=\"text-align: right;\">  iter</th><th style=\"text-align: right;\">  total time (s)</th><th style=\"text-align: right;\">    loss</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "<tr><td>train_function_wandb_877eb_00000</td><td>TERMINATED</td><td>127.0.0.1:14647</td><td style=\"text-align: right;\">     1</td><td style=\"text-align: right;\">0.738281</td><td style=\"text-align: right;\">    30</td><td style=\"text-align: right;\">         1.61319</td><td style=\"text-align: right;\">0.555153</td></tr>\n",
       "<tr><td>train_function_wandb_877eb_00001</td><td>TERMINATED</td><td>127.0.0.1:14660</td><td style=\"text-align: right;\">     2</td><td style=\"text-align: right;\">0.321178</td><td style=\"text-align: right;\">    30</td><td style=\"text-align: right;\">         1.72447</td><td style=\"text-align: right;\">2.52109 </td></tr>\n",
       "<tr><td>train_function_wandb_877eb_00002</td><td>TERMINATED</td><td>127.0.0.1:14661</td><td style=\"text-align: right;\">     3</td><td style=\"text-align: right;\">0.202487</td><td style=\"text-align: right;\">    30</td><td style=\"text-align: right;\">         1.8159 </td><td style=\"text-align: right;\">2.45412 </td></tr>\n",
       "<tr><td>train_function_wandb_877eb_00003</td><td>TERMINATED</td><td>127.0.0.1:14662</td><td style=\"text-align: right;\">     4</td><td style=\"text-align: right;\">0.515434</td><td style=\"text-align: right;\">    30</td><td style=\"text-align: right;\">         1.715  </td><td style=\"text-align: right;\">4.51413 </td></tr>\n",
       "<tr><td>train_function_wandb_877eb_00004</td><td>TERMINATED</td><td>127.0.0.1:14663</td><td style=\"text-align: right;\">     5</td><td style=\"text-align: right;\">0.216098</td><td style=\"text-align: right;\">    30</td><td style=\"text-align: right;\">         1.72827</td><td style=\"text-align: right;\">5.2814  </td></tr>\n",
       "</tbody>\n",
       "</table>\n",
       "  </div>\n",
       "</div>\n",
       "<style>\n",
       ".tuneStatus {\n",
       "  color: var(--jp-ui-font-color1);\n",
       "}\n",
       ".tuneStatus .systemInfo {\n",
       "  display: flex;\n",
       "  flex-direction: column;\n",
       "}\n",
       ".tuneStatus td {\n",
       "  white-space: nowrap;\n",
       "}\n",
       ".tuneStatus .trialStatus {\n",
       "  display: flex;\n",
       "  flex-direction: column;\n",
       "}\n",
       ".tuneStatus h3 {\n",
       "  font-weight: bold;\n",
       "}\n",
       ".tuneStatus .hDivider {\n",
       "  border-bottom-width: var(--jp-border-width);\n",
       "  border-bottom-color: var(--jp-border-color0);\n",
       "  border-bottom-style: solid;\n",
       "}\n",
       ".tuneStatus .vDivider {\n",
       "  border-left-width: var(--jp-border-width);\n",
       "  border-left-color: var(--jp-border-color0);\n",
       "  border-left-style: solid;\n",
       "  margin: 0.5em 1em 0.5em 1em;\n",
       "}\n",
       "</style>\n"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\u001b[2m\u001b[36m(train_function_wandb pid=14647)\u001b[0m 2022-11-02 16:03:17,149\tINFO wandb.py:282 -- Already logged into W&B.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div class=\"trialProgress\">\n",
       "  <h3>Trial Progress</h3>\n",
       "  <table>\n",
       "<thead>\n",
       "<tr><th>Trial name                      </th><th>date               </th><th>done  </th><th>episodes_total  </th><th>experiment_id                   </th><th>experiment_tag    </th><th>hostname            </th><th style=\"text-align: right;\">  iterations_since_restore</th><th style=\"text-align: right;\">    loss</th><th>node_ip  </th><th style=\"text-align: right;\">  pid</th><th style=\"text-align: right;\">  time_since_restore</th><th style=\"text-align: right;\">  time_this_iter_s</th><th style=\"text-align: right;\">  time_total_s</th><th style=\"text-align: right;\">  timestamp</th><th style=\"text-align: right;\">  timesteps_since_restore</th><th>timesteps_total  </th><th style=\"text-align: right;\">  training_iteration</th><th>trial_id   </th><th style=\"text-align: right;\">  warmup_time</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "<tr><td>train_function_wandb_877eb_00000</td><td>2022-11-02_16-03-18</td><td>True  </td><td>                </td><td>7b250c9f31ab484dad1a1fd29823afdf</td><td>0_mean=1,sd=0.7383</td><td>Kais-MBP.local.meter</td><td style=\"text-align: right;\">                        30</td><td style=\"text-align: right;\">0.555153</td><td>127.0.0.1</td><td style=\"text-align: right;\">14647</td><td style=\"text-align: right;\">             1.61319</td><td style=\"text-align: right;\">        0.00232315</td><td style=\"text-align: right;\">       1.61319</td><td style=\"text-align: right;\"> 1667430198</td><td style=\"text-align: right;\">                        0</td><td>                 </td><td style=\"text-align: right;\">                  30</td><td>877eb_00000</td><td style=\"text-align: right;\">   0.00391102</td></tr>\n",
       "<tr><td>train_function_wandb_877eb_00001</td><td>2022-11-02_16-03-22</td><td>True  </td><td>                </td><td>5172868368074557a3044ea3a9146673</td><td>1_mean=2,sd=0.3212</td><td>Kais-MBP.local.meter</td><td style=\"text-align: right;\">                        30</td><td style=\"text-align: right;\">2.52109 </td><td>127.0.0.1</td><td style=\"text-align: right;\">14660</td><td style=\"text-align: right;\">             1.72447</td><td style=\"text-align: right;\">        0.0152011 </td><td style=\"text-align: right;\">       1.72447</td><td style=\"text-align: right;\"> 1667430202</td><td style=\"text-align: right;\">                        0</td><td>                 </td><td style=\"text-align: right;\">                  30</td><td>877eb_00001</td><td style=\"text-align: right;\">   0.00901699</td></tr>\n",
       "<tr><td>train_function_wandb_877eb_00002</td><td>2022-11-02_16-03-22</td><td>True  </td><td>                </td><td>b13d9bccb1964b4b95e1a858a3ea64c7</td><td>2_mean=3,sd=0.2025</td><td>Kais-MBP.local.meter</td><td style=\"text-align: right;\">                        30</td><td style=\"text-align: right;\">2.45412 </td><td>127.0.0.1</td><td style=\"text-align: right;\">14661</td><td style=\"text-align: right;\">             1.8159 </td><td style=\"text-align: right;\">        0.00437403</td><td style=\"text-align: right;\">       1.8159 </td><td style=\"text-align: right;\"> 1667430202</td><td style=\"text-align: right;\">                        0</td><td>                 </td><td style=\"text-align: right;\">                  30</td><td>877eb_00002</td><td style=\"text-align: right;\">   0.00844812</td></tr>\n",
       "<tr><td>train_function_wandb_877eb_00003</td><td>2022-11-02_16-03-22</td><td>True  </td><td>                </td><td>869d7ec7a3544a8387985103e626818f</td><td>3_mean=4,sd=0.5154</td><td>Kais-MBP.local.meter</td><td style=\"text-align: right;\">                        30</td><td style=\"text-align: right;\">4.51413 </td><td>127.0.0.1</td><td style=\"text-align: right;\">14662</td><td style=\"text-align: right;\">             1.715  </td><td style=\"text-align: right;\">        0.00247812</td><td style=\"text-align: right;\">       1.715  </td><td style=\"text-align: right;\"> 1667430202</td><td style=\"text-align: right;\">                        0</td><td>                 </td><td style=\"text-align: right;\">                  30</td><td>877eb_00003</td><td style=\"text-align: right;\">   0.00282907</td></tr>\n",
       "<tr><td>train_function_wandb_877eb_00004</td><td>2022-11-02_16-03-22</td><td>True  </td><td>                </td><td>84d3112d66f64325bc469e44b8447ef5</td><td>4_mean=5,sd=0.2161</td><td>Kais-MBP.local.meter</td><td style=\"text-align: right;\">                        30</td><td style=\"text-align: right;\">5.2814  </td><td>127.0.0.1</td><td style=\"text-align: right;\">14663</td><td style=\"text-align: right;\">             1.72827</td><td style=\"text-align: right;\">        0.00517201</td><td style=\"text-align: right;\">       1.72827</td><td style=\"text-align: right;\"> 1667430202</td><td style=\"text-align: right;\">                        0</td><td>                 </td><td style=\"text-align: right;\">                  30</td><td>877eb_00004</td><td style=\"text-align: right;\">   0.00272107</td></tr>\n",
       "</tbody>\n",
       "</table>\n",
       "</div>\n",
       "<style>\n",
       ".trialProgress {\n",
       "  display: flex;\n",
       "  flex-direction: column;\n",
       "  color: var(--jp-ui-font-color1);\n",
       "}\n",
       ".trialProgress h3 {\n",
       "  font-weight: bold;\n",
       "}\n",
       ".trialProgress td {\n",
       "  white-space: nowrap;\n",
       "}\n",
       "</style>\n"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\u001b[2m\u001b[36m(train_function_wandb pid=14660)\u001b[0m 2022-11-02 16:03:20,600\tINFO wandb.py:282 -- Already logged into W&B.\n",
      "\u001b[2m\u001b[36m(train_function_wandb pid=14661)\u001b[0m 2022-11-02 16:03:20,600\tINFO wandb.py:282 -- Already logged into W&B.\n",
      "\u001b[2m\u001b[36m(train_function_wandb pid=14663)\u001b[0m 2022-11-02 16:03:20,628\tINFO wandb.py:282 -- Already logged into W&B.\n",
      "\u001b[2m\u001b[36m(train_function_wandb pid=14662)\u001b[0m 2022-11-02 16:03:20,723\tINFO wandb.py:282 -- Already logged into W&B.\n",
      "2022-11-02 16:03:22,565\tINFO tune.py:788 -- Total run time: 8.60 seconds (8.48 seconds for the tuning loop).\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div class=\"tuneStatus\">\n",
       "  <div style=\"display: flex;flex-direction: row\">\n",
       "    <div style=\"display: flex;flex-direction: column;\">\n",
       "      <h3>Tune Status</h3>\n",
       "      <table>\n",
       "<tbody>\n",
       "<tr><td>Current time:</td><td>2022-11-02 16:03:31</td></tr>\n",
       "<tr><td>Running for: </td><td>00:00:09.28        </td></tr>\n",
       "<tr><td>Memory:      </td><td>9.9/16.0 GiB       </td></tr>\n",
       "</tbody>\n",
       "</table>\n",
       "    </div>\n",
       "    <div class=\"vDivider\"></div>\n",
       "    <div class=\"systemInfo\">\n",
       "      <h3>System Info</h3>\n",
       "      Using FIFO scheduling algorithm.<br>Resources requested: 0/16 CPUs, 0/0 GPUs, 0.0/3.44 GiB heap, 0.0/1.72 GiB objects\n",
       "    </div>\n",
       "    \n",
       "  </div>\n",
       "  <div class=\"hDivider\"></div>\n",
       "  <div class=\"trialStatus\">\n",
       "    <h3>Trial Status</h3>\n",
       "    <table>\n",
       "<thead>\n",
       "<tr><th>Trial name                </th><th>status    </th><th>loc            </th><th style=\"text-align: right;\">  mean</th><th style=\"text-align: right;\">      sd</th><th style=\"text-align: right;\">  iter</th><th style=\"text-align: right;\">  total time (s)</th><th style=\"text-align: right;\">    loss</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "<tr><td>WandbTrainable_8ca33_00000</td><td>TERMINATED</td><td>127.0.0.1:14718</td><td style=\"text-align: right;\">     1</td><td style=\"text-align: right;\">0.397894</td><td style=\"text-align: right;\">     1</td><td style=\"text-align: right;\">     0.000187159</td><td style=\"text-align: right;\">0.742345</td></tr>\n",
       "<tr><td>WandbTrainable_8ca33_00001</td><td>TERMINATED</td><td>127.0.0.1:14737</td><td style=\"text-align: right;\">     2</td><td style=\"text-align: right;\">0.386883</td><td style=\"text-align: right;\">     1</td><td style=\"text-align: right;\">     0.000151873</td><td style=\"text-align: right;\">2.5709  </td></tr>\n",
       "<tr><td>WandbTrainable_8ca33_00002</td><td>TERMINATED</td><td>127.0.0.1:14738</td><td style=\"text-align: right;\">     3</td><td style=\"text-align: right;\">0.290693</td><td style=\"text-align: right;\">     1</td><td style=\"text-align: right;\">     0.00014019 </td><td style=\"text-align: right;\">2.99601 </td></tr>\n",
       "<tr><td>WandbTrainable_8ca33_00003</td><td>TERMINATED</td><td>127.0.0.1:14739</td><td style=\"text-align: right;\">     4</td><td style=\"text-align: right;\">0.33333 </td><td style=\"text-align: right;\">     1</td><td style=\"text-align: right;\">     0.00015831 </td><td style=\"text-align: right;\">3.91276 </td></tr>\n",
       "<tr><td>WandbTrainable_8ca33_00004</td><td>TERMINATED</td><td>127.0.0.1:14740</td><td style=\"text-align: right;\">     5</td><td style=\"text-align: right;\">0.645479</td><td style=\"text-align: right;\">     1</td><td style=\"text-align: right;\">     0.000150919</td><td style=\"text-align: right;\">5.47779 </td></tr>\n",
       "</tbody>\n",
       "</table>\n",
       "  </div>\n",
       "</div>\n",
       "<style>\n",
       ".tuneStatus {\n",
       "  color: var(--jp-ui-font-color1);\n",
       "}\n",
       ".tuneStatus .systemInfo {\n",
       "  display: flex;\n",
       "  flex-direction: column;\n",
       "}\n",
       ".tuneStatus td {\n",
       "  white-space: nowrap;\n",
       "}\n",
       ".tuneStatus .trialStatus {\n",
       "  display: flex;\n",
       "  flex-direction: column;\n",
       "}\n",
       ".tuneStatus h3 {\n",
       "  font-weight: bold;\n",
       "}\n",
       ".tuneStatus .hDivider {\n",
       "  border-bottom-width: var(--jp-border-width);\n",
       "  border-bottom-color: var(--jp-border-color0);\n",
       "  border-bottom-style: solid;\n",
       "}\n",
       ".tuneStatus .vDivider {\n",
       "  border-left-width: var(--jp-border-width);\n",
       "  border-left-color: var(--jp-border-color0);\n",
       "  border-left-style: solid;\n",
       "  margin: 0.5em 1em 0.5em 1em;\n",
       "}\n",
       "</style>\n"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\u001b[2m\u001b[36m(WandbTrainable pid=14718)\u001b[0m 2022-11-02 16:03:25,742\tINFO wandb.py:282 -- Already logged into W&B.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div class=\"trialProgress\">\n",
       "  <h3>Trial Progress</h3>\n",
       "  <table>\n",
       "<thead>\n",
       "<tr><th>Trial name                </th><th>date               </th><th>done  </th><th>episodes_total  </th><th>experiment_id                   </th><th>hostname            </th><th style=\"text-align: right;\">  iterations_since_restore</th><th style=\"text-align: right;\">    loss</th><th>node_ip  </th><th style=\"text-align: right;\">  pid</th><th style=\"text-align: right;\">  time_since_restore</th><th style=\"text-align: right;\">  time_this_iter_s</th><th style=\"text-align: right;\">  time_total_s</th><th style=\"text-align: right;\">  timestamp</th><th style=\"text-align: right;\">  timesteps_since_restore</th><th>timesteps_total  </th><th style=\"text-align: right;\">  training_iteration</th><th>trial_id   </th><th style=\"text-align: right;\">  warmup_time</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "<tr><td>WandbTrainable_8ca33_00000</td><td>2022-11-02_16-03-27</td><td>True  </td><td>                </td><td>3adb4d0ae0d74d1c9ddd07924b5653b0</td><td>Kais-MBP.local.meter</td><td style=\"text-align: right;\">                         1</td><td style=\"text-align: right;\">0.742345</td><td>127.0.0.1</td><td style=\"text-align: right;\">14718</td><td style=\"text-align: right;\">         0.000187159</td><td style=\"text-align: right;\">       0.000187159</td><td style=\"text-align: right;\">   0.000187159</td><td style=\"text-align: right;\"> 1667430207</td><td style=\"text-align: right;\">                        0</td><td>                 </td><td style=\"text-align: right;\">                   1</td><td>8ca33_00000</td><td style=\"text-align: right;\">      1.31382</td></tr>\n",
       "<tr><td>WandbTrainable_8ca33_00001</td><td>2022-11-02_16-03-31</td><td>True  </td><td>                </td><td>f1511cfd51f94b3d9cf192181ccc08a9</td><td>Kais-MBP.local.meter</td><td style=\"text-align: right;\">                         1</td><td style=\"text-align: right;\">2.5709  </td><td>127.0.0.1</td><td style=\"text-align: right;\">14737</td><td style=\"text-align: right;\">         0.000151873</td><td style=\"text-align: right;\">       0.000151873</td><td style=\"text-align: right;\">   0.000151873</td><td style=\"text-align: right;\"> 1667430211</td><td style=\"text-align: right;\">                        0</td><td>                 </td><td style=\"text-align: right;\">                   1</td><td>8ca33_00001</td><td style=\"text-align: right;\">      1.31668</td></tr>\n",
       "<tr><td>WandbTrainable_8ca33_00002</td><td>2022-11-02_16-03-31</td><td>True  </td><td>                </td><td>a7528ec6adf74de0b73aa98ebedab66d</td><td>Kais-MBP.local.meter</td><td style=\"text-align: right;\">                         1</td><td style=\"text-align: right;\">2.99601 </td><td>127.0.0.1</td><td style=\"text-align: right;\">14738</td><td style=\"text-align: right;\">         0.00014019 </td><td style=\"text-align: right;\">       0.00014019 </td><td style=\"text-align: right;\">   0.00014019 </td><td style=\"text-align: right;\"> 1667430211</td><td style=\"text-align: right;\">                        0</td><td>                 </td><td style=\"text-align: right;\">                   1</td><td>8ca33_00002</td><td style=\"text-align: right;\">      1.32008</td></tr>\n",
       "<tr><td>WandbTrainable_8ca33_00003</td><td>2022-11-02_16-03-31</td><td>True  </td><td>                </td><td>b7af756ca586449ba2d4c44141b53b06</td><td>Kais-MBP.local.meter</td><td style=\"text-align: right;\">                         1</td><td style=\"text-align: right;\">3.91276 </td><td>127.0.0.1</td><td style=\"text-align: right;\">14739</td><td style=\"text-align: right;\">         0.00015831 </td><td style=\"text-align: right;\">       0.00015831 </td><td style=\"text-align: right;\">   0.00015831 </td><td style=\"text-align: right;\"> 1667430211</td><td style=\"text-align: right;\">                        0</td><td>                 </td><td style=\"text-align: right;\">                   1</td><td>8ca33_00003</td><td style=\"text-align: right;\">      1.31879</td></tr>\n",
       "<tr><td>WandbTrainable_8ca33_00004</td><td>2022-11-02_16-03-31</td><td>True  </td><td>                </td><td>196624f42bcc45c18a26778573a43a2c</td><td>Kais-MBP.local.meter</td><td style=\"text-align: right;\">                         1</td><td style=\"text-align: right;\">5.47779 </td><td>127.0.0.1</td><td style=\"text-align: right;\">14740</td><td style=\"text-align: right;\">         0.000150919</td><td style=\"text-align: right;\">       0.000150919</td><td style=\"text-align: right;\">   0.000150919</td><td style=\"text-align: right;\"> 1667430211</td><td style=\"text-align: right;\">                        0</td><td>                 </td><td style=\"text-align: right;\">                   1</td><td>8ca33_00004</td><td style=\"text-align: right;\">      1.31945</td></tr>\n",
       "</tbody>\n",
       "</table>\n",
       "</div>\n",
       "<style>\n",
       ".trialProgress {\n",
       "  display: flex;\n",
       "  flex-direction: column;\n",
       "  color: var(--jp-ui-font-color1);\n",
       "}\n",
       ".trialProgress h3 {\n",
       "  font-weight: bold;\n",
       "}\n",
       ".trialProgress td {\n",
       "  white-space: nowrap;\n",
       "}\n",
       "</style>\n"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\u001b[2m\u001b[36m(WandbTrainable pid=14739)\u001b[0m 2022-11-02 16:03:30,360\tINFO wandb.py:282 -- Already logged into W&B.\n",
      "\u001b[2m\u001b[36m(WandbTrainable pid=14740)\u001b[0m 2022-11-02 16:03:30,393\tINFO wandb.py:282 -- Already logged into W&B.\n",
      "\u001b[2m\u001b[36m(WandbTrainable pid=14737)\u001b[0m 2022-11-02 16:03:30,454\tINFO wandb.py:282 -- Already logged into W&B.\n",
      "\u001b[2m\u001b[36m(WandbTrainable pid=14738)\u001b[0m 2022-11-02 16:03:30,510\tINFO wandb.py:282 -- Already logged into W&B.\n",
      "2022-11-02 16:03:31,985\tINFO tune.py:788 -- Total run time: 9.40 seconds (9.27 seconds for the tuning loop).\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'mean': 1, 'sd': 0.3978937765393781, 'wandb': {'project': 'Wandb_example'}}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import os\n",
    "\n",
    "mock_api = True\n",
    "\n",
    "if mock_api:\n",
    "    os.environ.setdefault(\"WANDB_MODE\", \"disabled\")\n",
    "    os.environ.setdefault(\"WANDB_API_KEY\", \"abcd\")\n",
    "    ray.init(\n",
    "        runtime_env={\"env_vars\": {\"WANDB_MODE\": \"disabled\", \"WANDB_API_KEY\": \"abcd\"}}\n",
    "    )\n",
    "\n",
    "tune_with_callback()\n",
    "tune_with_setup()\n",
    "tune_trainable()\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "2f6e9138",
   "metadata": {},
   "source": [
    "This completes our Tune and Wandb walk-through.\n",
    "In the following sections you can find more details on the API of the Tune-Wandb integration.\n",
    "\n",
    "## Tune Wandb API Reference\n",
    "\n",
    "### WandbLoggerCallback\n",
    "\n",
    "(air-wandb-logger)=\n",
    "\n",
    "```{eval-rst}\n",
    ".. autoclass:: ray.air.integrations.wandb.WandbLoggerCallback\n",
    "   :noindex:\n",
    "```\n",
    "\n",
    "### setup_wandb\n",
    "\n",
    "(air-wandb-setup)=\n",
    "\n",
    "```{eval-rst}\n",
    ".. autofunction:: ray.air.integrations.wandb.setup_wandb\n",
    "   :noindex:\n",
    "```"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  },
  "orphan": true
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
